home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / prolog / sbprolog / amiga / v3_1 / sbp3_1e.lzh / CAR.PL < prev    next >
Text File  |  1991-10-31  |  4KB  |  139 lines

  1. /* From the book PROLOG PROGRAMMING IN DEPTH
  2.    by Michael A. Covington, Donald Nute, and Andre Vellino.
  3.    Copyright 1988 Scott, Foresman & Co.
  4.    Non-commercial distribution of this file is permitted. */
  5.  
  6. /* Modified for Quintus Prolog by Andreas Siebert */
  7.  
  8. /* CAR.PL */
  9. /* Simple expert system */
  10.  
  11. /*
  12.  * User interface and control routines
  13.  */
  14.  
  15. start :-
  16.    nl,
  17.    write('This program diagnoses why a car won''t start.'),nl,
  18.    write('Answer all questions with ''yes.'' or ''no.'''),nl,
  19.    write('(lower case, with period, without quotes).'),nl,
  20.    try_all_possibilities.
  21.  
  22. try_all_possibilities :-
  23.    /* The 'fail' at the end causes this predicate to */
  24.    /* backtrack through all possibilities.           */
  25.    defect_may_be(D),
  26.    explain(D),
  27.    fail.
  28.  
  29. try_all_possibilities.
  30.    /* If all paths through the previous definition */
  31.    /* have been exhausted, succeed and quit.       */
  32.  
  33. /*
  34.  * Diagnostic knowledge base: a set of diagnoses and
  35.  *  the conditions under which each one is to be given
  36.  */
  37.  
  38. defect_may_be(drained_battery) :-
  39.    starter_was_ok(yes),
  40.    starter_is_ok(no).
  41.  
  42. defect_may_be(wrong_gear) :-
  43.    starter_was_ok(no).
  44.  
  45. defect_may_be(starting_system) :-
  46.    starter_was_ok(no).
  47.  
  48. defect_may_be(fuel_system) :-
  49.    starter_was_ok(yes),
  50.    fuel_is_ok(no).
  51.  
  52. defect_may_be(ignition_system) :-
  53.    starter_was_ok(yes),
  54.    fuel_is_ok(yes).
  55.  
  56. /*
  57.  * Working database
  58.  *   Several items of information are collected from the user
  59.  *   if and when they are needed, and added to the knowledge base.
  60.  *   All start out with the value 'unknown'.
  61.  */
  62.  
  63. starter_was_ok(unknown).
  64.  
  65. starter_was_ok(X) :-
  66.    retract(starter_was_ok(unknown)),
  67.    nl,
  68.    write('When you first started trying to start the car,'),nl,
  69.    write('did the starter crank the engine normally? '),
  70.    read(Reply),
  71.    asserta(starter_was_ok(Reply)),
  72.    X = Reply.
  73.  
  74. starter_is_ok(unknown).
  75.  
  76. starter_is_ok(no) :-
  77.    starter_was_ok(no).
  78.  
  79. starter_is_ok(X) :-
  80.    retract(starter_is_ok(unknown)),
  81.    nl,
  82.    write('Does the starter crank the engine normally now? '),
  83.    read(Reply),
  84.    asserta(starter_is_ok(Reply)),
  85.    X = Reply.
  86.  
  87. fuel_is_ok(unknown).
  88.  
  89. fuel_is_ok(X) :-
  90.    retract(fuel_is_ok(unknown)),
  91.    nl,
  92.    write('Is fuel being delivered to the carburetor? '),
  93.    read(Reply),
  94.    asserta(fuel_is_ok(Reply)),
  95.    X = Reply.
  96.  
  97. /*
  98.  *  Explanations for the various diagnoses
  99.  */
  100.  
  101. explain(wrong_gear) :-
  102.    nl,
  103.    write('Check that the gearshift is set to Park or Neutral.'),nl,
  104.    write('Try jiggling the gearshift lever.'),nl.
  105.  
  106. explain(starting_system) :-
  107.    nl,
  108.    write('Check for a defective battery, voltage'),nl,
  109.    write('regulator, or alternator; if any of these is'),nl,
  110.    write('the problem, charging the battery or jump-'),nl,
  111.    write('starting may get the car going temporarily.'),nl,
  112.    write('Alternatively, the starter itself may be'),nl,
  113.    write('defective.'),nl.
  114.  
  115. explain(drained_battery) :-
  116.    nl,
  117.    write('The battery has apparently become drained during'),nl,
  118.    write('your attempts to start the car. Recharging or'),nl,
  119.    write('jump-starting will be necessary. However, there'),nl,
  120.    write('is probably nothing wrong with the battery itself.'),nl.
  121.  
  122. explain(fuel_system) :-
  123.    nl,
  124.    write('Check whether there is fuel in the tank.'),nl,
  125.    write('If so, check for a clogged fuel line or filter'),nl,
  126.    write('or a defective fuel pump.'),nl.
  127.  
  128. explain(ignition_system) :-
  129.    nl,
  130.    write('Check the spark plugs, cables, distributor,'),nl,
  131.    write('coil, and other parts of the ignition system.'),nl,
  132.    write('If any of these are visibly defective or long'),nl,
  133.    write('overdue for replacement, replace them; if this'),nl,
  134.    write('does not solve the problem, consult a mechanic.'),nl.
  135.  
  136. /*
  137.  * Starting query
  138.  */
  139.